﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMM.Tapioca.Linkbase;
using System.Xml.Linq;

namespace WMM.Tapioca.Formula
{
    /// <summary>
    /// The base class for all formula classes.
    /// </summary>
    public class FormulaResource
    {
        private string resourceName;

        /// <returns>The filter name. </returns>
        public string ResourceName
        {
            get { return resourceName; }
            set { resourceName = value; }
        }

        private string id;

        /// <returns> The attribute "id". </returns>
        public string Id
        {
            get { return id; }
            set { id = value; }
        }

        private string type;

        /// <summary>
        /// The type of resource: arc or resource.
        /// </summary>
        public string Type
        {
            get { return type; }
            set { type = value; }
        }

        private string label;

        /// <summary>
        /// The attribute "label" of the resource.
        /// </summary>
        public string Label
        {
            get { return label; }
            set { label = value; }
        }

        private string role;

        /// <returns> The role of resource. </returns>
        public string Role
        {
            get { return role; }
            set { role = value; }
        }

        private XElement resourceXElement;

        public XElement ResourceXElement
        {
            get { return resourceXElement; }
            set { resourceXElement = value; }
        }

        /// <summary>
        /// Constructor with resource as parameter.
        /// </summary>
        public FormulaResource(Resource resource) 
        {
            ResourceName = resource.ResourceXElement.Name.LocalName;
            label = resource.Label;
            role = resource.Role;
            resourceXElement = resource.ResourceXElement;

            foreach (XAttribute attr in resource.ResourceXElement.Attributes())
            {
                if (attr.Name.LocalName.Equals("type"))
                {
                    type = attr.Value;
                }

                if (attr.Name.LocalName.Equals("id"))
                {
                    id = attr.Value;
                }
            }
        }

        /// <summary>
        /// Constructor with arc as paraneter.
        /// </summary>
        public FormulaResource(Arc arc)
        {
            foreach (XAttribute attr in arc.ArcElement.Attributes())
            {
                if (attr.Name.LocalName.Equals("type"))
                {
                    type = attr.Value;
                }

                if (attr.Name.LocalName.Equals("id"))
                {
                    id = attr.Value;
                }
            }
        }

        public FormulaResource() { }
    }
}
